package com.hourse.app.service.impl;

import com.hourse.app.service.CurrencyOrderService;
import com.hourse.app.service.ExternalAccountService;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.impl.PublicServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;

/**
 * @作者：lichenguang
 * @描述：【订单】
 * @创建：2021/12/30 20:59
 */
@Service
@Slf4j
public class CurrencyOrderServiceImpl extends PublicServiceImpl implements CurrencyOrderService {


  @Autowired
  private ExternalAccountService externalAccountService;

  /**
   * @描述：【委托下单】
   * @作者：lichenguang
   * @时间: 2021/12/31 15:33
   */
  @Override
  @Transactional(rollbackFor = AppException.class)
  public Object order(String amount, String currency, String price, String tradeType, String platform) throws AppException, CloneNotSupportedException, InvalidKeyException, UnsupportedEncodingException {
    //
    //  CurrencyOrderEntity currencyOrderEntity = new CurrencyOrderEntity();
    //  currencyOrderEntity.setAmount(amount);
    //  currencyOrderEntity.setCurrency(currency);
    //  currencyOrderEntity.setPrice(price);
    //  currencyOrderEntity.setTradeType(tradeType);
    //  currencyOrderEntity.setCustomerOrderId(RandomTools.createOrderNo());
    //
    //  CurrencyKindEntity entity = currencyKindService.byCode(currency);
    //  Platform factory = PlatformFactory.getPlatform(platform);
    //  Object balance = factory.order(amount, entity, CurrentThread.getUKey(platform), CurrentThread.getuSecret(platform), price, tradeType);
    //
    //  //JSONObject order;
    //  //switch (platform) {
    //  //  case BourseTagConstant.ZB_PLATFORM:
    //  //    order = ZbRequestTool.order(amount, entity.getZbCode(), price, tradeType);
    //  //    log.info("委托下单结果", order);
    //  //    currencyOrderEntity.setOrderId(order.getString("id"));
    //  //    break;
    //  //  case BourseTagConstant.OKEX_PLATFORM:
    //  //    //order = OkexRequesTools.order(entity.getOyCode(), "isolated", tradeType, "market", amount, price);
    //  //    //log.info("委托下单结果", order);
    //  //    //currencyOrderEntity.setOrderId(order.getString("id"));
    //  //    break;
    //  //  default:
    //  //    throw new AppException("暂不支持该三方平台");
    //  //}
    //
    //
    //  try {
    //    baseDao.saveObject(currencyOrderEntity);
    //  } catch (Exception e) {
    //    e.printStackTrace();
    //    throw new AppException("存储委托下单信息失败");
    //  }
    //  return null;
    //return order;
    return null;
  }

  /**
   * @描述：【获取账户信息】
   * @作者：lichenguang
   * @时间: 2022/1/12 20:08
   */
  @Override
  @Transactional(rollbackFor = AppException.class)
  public Object getZbAccountInfo(String platform) throws AppException {
    //
    //Platform factory = PlatformFactory.getPlatform(platform);
    //Object balance = factory.balance(CurrentThread.getUKey(platform), CurrentThread.getuSecret(platform));
    //
    //return balance;
    return null;
  }

  /**
   * @描述：【获取历史委托信息】
   * @作者：lichenguang
   * @时间: 2022/1/3 15:18
   */
  @Override
  public Object getOrders(String currency, String platform) throws AppException {
    //CurrencyKindEntity entity = currencyKindService.byCode(currency);
    //
    //Platform factory = PlatformFactory.getPlatform(platform);
    //Object balance = factory.getOrders(entity, CurrentThread.getUKey(platform), CurrentThread.getuSecret(platform));
    //
    //return balance;
    return null;
  }

  /**
   * @描述：【获取当天委托记录】
   * @作者：lichenguang
   * @时间: 2022/1/12 20:07
   */
  @Override
  public Object getNowDateOrders(String currency, String platform) throws AppException {

    //CurrencyKindEntity entity = currencyKindService.byCode(currency);
    //Platform factory = PlatformFactory.getPlatform(platform);
    //List<OrdersRetVo> balance = (List<OrdersRetVo>) factory.getOrdersPending(entity, CurrentThread.getUKey(platform), CurrentThread.getuSecret(platform));
    //return balance;
    return null;
  }

  /**
   * @描述：【获取成交记录】
   * @作者：lichenguang
   * @时间: 2022/1/12 20:07
   */
  @Override
  public Object getFinishedAndPartialOrders(String currency, String pageIndex, String pageSize, String platform) throws AppException {

    //CurrencyKindEntity entity = currencyKindService.byCode(currency);
    //Platform factory = PlatformFactory.getPlatform(platform);
    //Object list = factory.fillsHistory(entity, CurrentThread.getUKey(platform), CurrentThread.getuSecret(platform), pageIndex, pageSize);
    //
    //return list;
    return null;
  }

  /**
   * @描述：【获取可用币种余额】
   * @作者：lichenguang
   * @时间: 2022/1/12 20:41
   */
  @Override
  public Object getUsableCurrency(String currency, String platform) throws AppException, IOException {

    //CurrencyKindEntity entity = currencyKindService.byCode(currency);
    //Platform factory = PlatformFactory.getPlatform(platform);
    //String usable = (String) factory.getUsableCurrency(entity, CurrentThread.getUKey(platform), CurrentThread.getuSecret(platform));
    //
    //BigDecimal value = new BigDecimal(usable).setScale(2, BigDecimal.ROUND_DOWN);
    //// 不足两位小数补0
    //DecimalFormat decimalFormat = new DecimalFormat("0.00#");
    //usable = decimalFormat.format(value);
    //
    //return usable;
    return null;
  }
}
